/*
 * Copyright (c) 2021-2021. Gardel
 *
 * This file is part of Source2Pdf (https://gitee.com/gardel/source2pdf).
 *
 * Source2Pdf is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Source2Pdf is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Source2Pdf.  If not, see <http://www.gnu.org/licenses/>.
 */

package cn.sunxinao.source2pdf.utils;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class TaskUtils {
    private static final ExecutorService executorService;

    static {
        executorService = new ThreadPoolExecutor(5, 10,
            120L, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
    }

    private TaskUtils() {
        throw new IllegalStateException();
    }

    public static void submitTask(Runnable runnable) {
        executorService.submit(runnable);
    }

    public static <T> Future<T> submitTask(Callable<T> callable) {
        return executorService.submit(callable);
    }

    public static boolean shutdown() throws InterruptedException {
        executorService.shutdown();
        executorService.shutdownNow();
        return executorService.awaitTermination(5, TimeUnit.SECONDS);
    }
}
